//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.pss;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.PssProductStockDao;
import com.aidong.dao.wills.PssPurcReturnDao;
import com.aidong.dao.wills.PssPurcReturnListDao;
import com.aidong.model.wills.Products;
import com.aidong.model.wills.PssPopProduct;
import com.aidong.model.wills.PssProductCodeAndProvider;
import com.aidong.model.wills.PssPurcReturn;
import com.aidong.model.wills.PssPurcReturnImpl;
import com.aidong.model.wills.PssPurcReturnList;
import com.aidong.service.PssPrintDataService;
import com.aidong.util.ProductUtil;
import com.aidong.web.controller.pss.PopProductController;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"purcReturn"})
public class PurcReturnController extends PopProductController {
    @Resource
    PssPurcReturnDao pssPurcReturnDao;
    @Resource
    PssPurcReturnListDao pssPurcReturnListDao;
    @Resource
    PssProductStockDao pssProductStockDao;
    @Resource
    ProductUtil productUtil;
    @Resource
    PssPrintDataService pssprintDataService;

    public PurcReturnController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("pss/purcreturn/list");
        return mv;
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "returnDate",required = false) String returnDate, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        cd.eq("ReturnDate", this.parseDate(returnDate, "yyyy-MM-dd"));
        this.pssPurcReturnDao.queryPageList(pg, cd, (new OrderSet()).asc("ReturnID"));
        ModelAndView mv = new ModelAndView("pss/purcreturn/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"add"})
    public ModelAndView add(@RequestParam("returnDate") String returnDate) {
        PssPurcReturnImpl pssPurcReturnImpl = new PssPurcReturnImpl();
        pssPurcReturnImpl.setReturnDate(returnDate);
        ModelAndView mv = new ModelAndView("pss/purcreturn/edit");
        mv.addObject("pssPurcReturn", pssPurcReturnImpl);
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("pssPurcReturn") PssPurcReturnImpl pssPurcReturn, Products products) {
        if(this.chkNull(pssPurcReturn.getReturnDate())) {
            return this.getResult(false, "请输入退货日期");
        } else {
            try {
                PssPurcReturn e = new PssPurcReturn();
                e.setReturnDate(this.parseDate(pssPurcReturn.getReturnDate(), "yyyy-MM-dd"));
                e.setShopCode(this.getLoginShopCode());
                e.setPrintLabel("N");
                e.setCreateuser(this.getLoginUserCode());
                e.setCreatetime(new Date());
                e.setUpdatetime(new Date());
                PssPurcReturnList pss = null;
                PssProductCodeAndProvider product = null;
                ArrayList lst = new ArrayList();
                Iterator i$ = products.getLstProduct().iterator();

                while(i$.hasNext()) {
                    PssPopProduct pc = (PssPopProduct)i$.next();
                    pss = new PssPurcReturnList();
                    if(!this.chkNull(pc.getProductCode())) {
                        product = this.productUtil.getProductInfo(pc.getProductCode());
                        pss.setProductCode(pc.getProductCode());
                        pss.setPrice(product.getPurcPrice());
                        pss.setProviderCode(product.getProviderCode());
                        pss.setQuantity(pc.getQty());
                        lst.add(pss);
                    }
                }

                this.pssPurcReturnDao.addPurcReturn(e, this.pssPurcReturnListDao, this.pssProductStockDao, lst);
                this.addUserLog("新增采购退货的商品", "退货单号:" + pssPurcReturn.getReturnID());
                return this.getResult(true, "新增采购退货的商品完成");
            } catch (Exception var9) {
                this.log.error("add purcReturn", var9);
                return this.getResult(false, "新增采购退货的商品发生错误");
            }
        }
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("returnID") Integer returnID) {
        PssPurcReturn pssPurcReturn = (PssPurcReturn)this.pssPurcReturnDao.getById(returnID);
        if(pssPurcReturn == null) {
            return this.showAlert("没有这个退货单号的商品");
        } else if("Y".equals(pssPurcReturn.getPrintLabel())) {
            return this.showAlert("退货单已经打印完成，不能修改退货数");
        } else {
            ConditionMap cd = new ConditionMap();
            cd.eq("ReturnID", returnID);
            List lst = this.pssPurcReturnListDao.queryPurcReturnRptList(cd);
            ModelAndView mv = new ModelAndView("pss/purcreturn/edit");
            mv.addObject("pssPurcReturn", pssPurcReturn);
            mv.addObject("list", lst);
            mv.addObject("productUnit", this.getTypeList("productUnit"));
            mv.addObject("_oper", "edit");
            return mv;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("pssPurcReturn") PssPurcReturnImpl pssPurcReturn, Products products) {
        try {
            this.pssPurcReturnDao.updatePurcReturn(this.pssPurcReturnListDao, this.pssProductStockDao, pssPurcReturn.getShopCode(), products);
            this.addUserLog("修改采购退货的商品", "退货单号:" + pssPurcReturn.getReturnID());
        } catch (Exception var4) {
            this.log.error("edit purcReturn", var4);
            return this.getResult(false, "修改采购退货的商品发生错误");
        }

        return this.getResult(true, "修改采购退货的商品完成");
    }

    @RequestMapping({"print"})
    public ModelAndView print(@RequestParam("returnID") Integer returnID) {
        StringBuilder msg = new StringBuilder();
        String data = this.pssprintDataService.getPssReturnData(returnID, msg);
        if(data == null) {
            return this.showAlert(msg.toString());
        } else {
            ModelAndView mv = new ModelAndView("pss/print/print");
            mv.addObject("data", data);
            mv.addObject("title", "采购退货单打印");
            mv.addObject("ptjs", "pssreturn");
            return mv;
        }
    }
}
